package by.epam.trenings.vkphotorest.service.handlers.impl;

import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.ResultReceiver;

import com.google.gson.Gson;

import by.epam.trenings.vkphotorest.content.ApplicationProvider;
import by.epam.trenings.vkphotorest.content.bo.AlbumModel;
import by.epam.trenings.vkphotorest.content.bo.AlbumModelListContainer;
import by.epam.trenings.vkphotorest.content.bo.PhotoModel;
import by.epam.trenings.vkphotorest.content.bo.PhotoModelListContainer;
import by.epam.trenings.vkphotorest.content.contract.TaskContract;
import by.epam.trenings.vkphotorest.response.common.CommonAsyncTask;
import by.epam.trenings.vkphotorest.response.common.CallbackAsyncTask;
import by.epam.trenings.vkphotorest.service.ApplicationService;
import by.epam.trenings.vkphotorest.service.handlers.BaseHandler;

public class DbInsertHandler extends BaseHandler {

	public static final String EXTRA_CURSOR_COUNT = "Cursor.getCount()";

	@Override
	public void doExecute(final Intent intent, final Context context,
			ResultReceiver callback) {
		String url = intent.getStringExtra(ApplicationService.EXTRA_URL);

		new CommonAsyncTask<String>(new CallbackAsyncTask<String>() {

			@Override
			public void onSuccess(final String obj) {
				if (obj == null || obj.equals("")) {
					Bundle data = new Bundle();
					data.putInt(EXTRA_CURSOR_COUNT, 0);
					sendUpdate(SUCCESS_RESPONSE, data);
				} else {
					new Thread(new Runnable() {
						@Override
						public void run() {
							Uri uri = Uri.parse(intent
									.getStringExtra(ApplicationService.EXTRA_URI));
							if (uri.equals(TaskContract.AlbumsColumns.CONTENT_URI)) {

								Gson gson = new Gson();
								AlbumModelListContainer gsonDeserialised = gson
										.fromJson(obj,
												AlbumModelListContainer.class);
								List<AlbumModel> albumsList = gsonDeserialised
										.getList();

								for (AlbumModel album : albumsList) {
									ContentValues contentValues = new ContentValues();
									contentValues.put(
											ApplicationProvider.EXTRA_DATA,
											new Gson().toJson(album));
									context.getContentResolver().insert(uri,
											contentValues);

								}
								Bundle data = new Bundle();
								data.putInt(EXTRA_CURSOR_COUNT,
										albumsList.size());
								sendUpdate(SUCCESS_RESPONSE, data);
							} else if (uri
									.equals(TaskContract.PhotosColumns.CONTENT_URI)) {

								Gson gson = new Gson();
								PhotoModelListContainer gsonDeserialised = gson
										.fromJson(obj,
												PhotoModelListContainer.class);
								List<PhotoModel> photosList = gsonDeserialised
										.getList();

								for (PhotoModel photo : photosList) {
									ContentValues contentValues = new ContentValues();
									contentValues.put(
											ApplicationProvider.EXTRA_DATA,
											new Gson().toJson(photo));

									context.getContentResolver().insert(uri,
											contentValues);
								}
								Bundle data = new Bundle();
								data.putInt(EXTRA_CURSOR_COUNT,
										photosList.size());
								sendUpdate(SUCCESS_RESPONSE, data);
							} else {
								Bundle data = new Bundle();
								data.putString(
										ApplicationService.ACTION_ON_ERROR,
										"Unknown URI!");
								sendUpdate(FAILURE_RESPONSE, data);
							}
						}
					}).start();
				}
			}

			@Override
			public void onError(Exception e) {
				Bundle data = new Bundle();
				data.putString(ApplicationService.ACTION_ON_ERROR,
						e.getMessage());
				sendUpdate(FAILURE_RESPONSE, data);
			}
		}) {
		}.start(url);
	}
}
